package com.itheima.case2.service;

import com.itheima.case2.dao.UserDao;
import com.itheima.case2.pojo.vo.AddUser;
import com.itheima.case2.pojo.vo.PageResult;
import com.itheima.case2.pojo.vo.QueryPageBean;
import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.pojo.po.User;
import com.itheima.case2.pojo.vo.UpdateUser;
import com.itheima.case2.utils.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class UserServiceImpl implements UserService {

    @Override
    public PageResult findListByPage(QueryPageBean queryPageBean) {
        SqlSession sqlSession = SqlSessionUtil.getSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        // 查询总记录数
        Long total = userDao.findTotalCount(queryPageBean);
        // 查询分页显示数据
        List<User> userList = userDao.findListByPage(queryPageBean);
        // 查询每个用户对应的角色
        for (User user : userList) {
            String uid = user.getId();
            Set<Role> set =  userDao.findRoleByUserId(uid);
            user.setRoles(set);
        }

        return new PageResult(total, userList);
    }

    public void addUser(AddUser user) {
        SqlSession sqlSession = SqlSessionUtil.getSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {
            //添加用户,并返回被影响的行数(执行之后,此用户的数据表主键字段也会设置给user对象的id)
            Integer count = userDao.addUser(user);
            //添加此用户对应的角色id到中间表中
            userDao.addUserRoles(user.getId(),user.getRoleIds());

            //影响的行数为0,说明添加失败
            if(count==0) {
                throw new RuntimeException("添加用户失败!");
            }
            SqlSessionUtil.commitAndClose(sqlSession);
        } catch (RuntimeException e) {
            SqlSessionUtil.rollbackAndClose(sqlSession);
            throw new RuntimeException(e.getMessage());
        }

    }

    public void updateUser(UpdateUser user) {
        SqlSession sqlSession = SqlSessionUtil.getSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {
            //修改用户信息
            Integer count = userDao.updateUser(user);
            //修改用户角色: 先删除原先所有的角色,再添加新角色
            userDao.deleteRoleById(user.getId());
            userDao.addUserRoles(user.getId(),user.getRoleIds());

            if(count==0) {
                throw new RuntimeException("修改用户失败!");
            }
            SqlSessionUtil.commitAndClose(sqlSession);
        } catch (RuntimeException e) {
            SqlSessionUtil.rollbackAndClose(sqlSession);
            throw new RuntimeException(e.getMessage());
        }
    }

    public void deleteUser(Integer id) {
        SqlSession sqlSession = SqlSessionUtil.getSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        try {
            //先删除此用户对应的所有角色,再删除角色本身
            userDao.deleteRoleById(id);
            Integer count = userDao.deleteUserById(id);

            if(count==0) {
                throw new RuntimeException("根据id删除用户失败!");
            }
            SqlSessionUtil.commitAndClose(sqlSession);
        } catch (RuntimeException e) {
            SqlSessionUtil.rollbackAndClose(sqlSession);
            throw new RuntimeException(e.getMessage());
        }
    }
}
